package com.idenger.modules.cms.mobileWeb.cms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.cms.service.ArticleService;
import com.idenger.modules.cms.service.CategoryService;
import com.idenger.modules.edu.entity.banner.EduBanner;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.service.banner.EduBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.idenger.common.persistence.Page;
import com.idenger.modules.cms.entity.Article;
import com.idenger.modules.cms.entity.Category;
import com.idenger.modules.cms.service.ArticleDataService;
import org.springframework.web.bind.annotation.RequestParam;

/***
 * 内容管理模块前端接口
 * @author lan
 *
 */
@Controller
@RequestMapping(value = "${mobilePath}/cms/eduCms")
public class McmsController extends BaseController {
	@Autowired
	private CategoryService categoryService;//分类
	@Autowired
	private ArticleService articleService;//文章
	@Autowired
	private ArticleDataService articleDataService;//文章内容

	@Autowired
	private EduBannerService eduBannerService;//banner
	
	//查询文章的类目
	@RequestMapping(value = "tree")
	public String tree(HttpServletRequest request, HttpServletResponse response, Model model) {
		BaseResponseMsg baseResponseMsg;
		List<Category> parents = categoryService.findByUser(true, null);
		List<Category> list = new ArrayList<>();
		for(Category category :parents){
			List<Category> subordinates = categoryService.findList(category);
			category.setSubordinates(subordinates);
			list.add(category);
		}
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(list);
		return renderString(response, baseResponseMsg);
	}



	@RequestMapping(value = "findFirstMenuAndChildMenu")
	public String findFirstMenuAndChildMenu(HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		List<Category> parents = categoryService.findFirstMenuAndChildMenu();
		for(Category category :parents){
			List<Category> subordinates = categoryService.findSubordinate(category.getId());
			category.setSubordinates(subordinates);
		}
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(parents);
		return renderString(response, baseResponseMsg);
	}




	//查询一个分类下的子分类
	@RequestMapping(value = "findChildren")
	public String findChildren(HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		String categoryId = request.getParameter("categoryId");
		List<Category> subordinates = categoryService.findSubordinate(categoryId);
		Map map = new HashMap<>();
		map.put("subordinates", subordinates);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(map);
		return renderString(response, baseResponseMsg);
	}

	//一个文章类目下的文章列表
	@RequestMapping(value = "list")
	public String list(Article article, HttpServletRequest request, HttpServletResponse response, Model model) {
		BaseResponseMsg baseResponseMsg;
		String categoryId = request.getParameter("categoryId");
		//List<Category> subordinates = categoryService.findSubordinate(categoryId);
		Category category = new Category();
		category.setId(categoryId);
		article.setCategory(category);
		//pageForPageSize用作设置page的分页数量
		Page<Article> pageForPageSize = new Page<>(request, response);
		pageForPageSize.setPageSize(5);
		Page<Article> page = articleService.findPage(pageForPageSize, article, true);
		List<Article> list = page.getList();
		List<Article> listForContent = new ArrayList<>();
		for(Article article2 :list){
			article2.setArticleData(articleDataService.get(article2.getId()));
			listForContent.add(article2);
		}
		Map map = new HashMap<>();
		page.setList(listForContent);
		map.put("page", page);
	//	map.put("subordinates", subordinates);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(map);
		return renderString(response, baseResponseMsg);
	}

	//根据题目查询文章列表
	@RequestMapping(value = "findArticleListByTitle")
	public String findArticleListByTitle(Article article, @RequestParam(required=false) String title,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		if(StringUtils.isNotBlank(title)){
			article.setTitle(title);
		}
		//pageForPageSize用作设置page的分页数量
		Page<Article> pageForPageSize = new Page<>(request, response);
		pageForPageSize.setPageSize(5);
		Page<Article> page = articleService.findPageByTitle(pageForPageSize, article, true);
		List<Article> list = page.getList();
		List<Article> listForContent = new ArrayList<>();
		for(Article article2 :list){
			article2.setArticleData(articleDataService.get(article2.getId()));
			listForContent.add(article2);
		}
		Map map = new HashMap<>();
		page.setList(listForContent);
		map.put("page", page);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(map);
		return renderString(response, baseResponseMsg);
	}




	@RequestMapping(value = "form")
	public String get(HttpServletRequest request, HttpServletResponse response, Model model){
		String id = request.getParameter("id");
		Article article = articleService.get(id);
		article.setArticleData(articleDataService.get(article.getId()));
		BaseResponseMsg baseResponseMsg;
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(article);
		return renderString(response, baseResponseMsg);
	}

	@RequestMapping(value = "getRecommend")
	public String getRecommend(HttpServletRequest request, HttpServletResponse response, Model model){
		Article article = articleService.findArticleForRemarks("1");
		BaseResponseMsg baseResponseMsg;
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(article);
		return renderString(response, baseResponseMsg);
	}

	@RequestMapping(value = "bannerList")
	public String bannerList(EduBanner eduBanner,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		List<EduBanner> eduBannerList = eduBannerService.findList(eduBanner);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(eduBannerList);
		return renderString(response, baseResponseMsg);
	}
	
}
